<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel fredbird@fredbird.org

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* webappkit service parent class
* @author J.Ducastel <fredbird@fredbird.org>
* @version 
*/
class wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	*/
	function wakService() {
		$this->__construct();
	}/**/
	
	/**
	*
	* @access
	*/
	function __construct() {
		// static
		// $this->service=get_class($this);
		$this->_setServiceId();
		$this->instance=uniqid($this->service.'-instance-');
		$this->_loadConfig();
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/

	/**
	* merges recursively array_1 with array_2, replacing array_1 keys by array_2 keys on collisions.
	* @param array &$array_1
	* @param array &$array_2
	* @return &array merged result
	*/
	function & array_merge_recursive_replace(&$array_1,&$array_2) {
		$merge=$array_1;
		$keys_1=array_keys($array_1);
		$keys_2=array_keys($array_2);
		foreach ($merge as $key => $value) {
			if (!isset($array_2[$key]))
				continue;
			if (is_array($value) and is_array($array_2[$key])) {
				// appel recursif
				$merge[$key]=$this->array_merge_recursive_replace($value,$array_2[$key]);
			} else {
				$merge[$key]=$array_2[$key];
			}
		}
		foreach ($array_2 as $key => $value)
			if (!isset($merge[$key])) $merge[$key]=$value;
		return $merge;
	}
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	var $service; // service name
	var $version; // service version
	var $cfg=array(); // configuration data
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* sets $service and $version from class name (theorically {service}_{version} )
	* @access private
	* @return bool
	*/
	function _setServiceId() {
		$class=get_class($this);
		$chunks=explode('_',$class);
		$this->service=array_shift($chunks);
		$this->version=join('_',$chunks);
		return true;
	}/**/

	/**
	* loads config from ini file(s)
	* searches for cfg/{version}.{service}.wakservice.cfg.ini, cfg/{service}.wakservice.cfg.ini, {core-path}/{version}.{service}.wakservice.cfg.ini, {core-path}/{service}.wakservice.cfg.ini
	* @access private
	* @return bool
	*/
	function _loadConfig() {
		$s=&$this->service;
		$v=&$this->version;
		// defining possible config files by priority order
		// app service version
		$files[]="app/cfg/$v.$s.wakservice.cfg.ini";
		// app service generic
		$files[]="app/cfg/$s.wakservice.cfg.ini";
		// core service version
		$files[]=WAK_CORE_PATH."cfg/$v.$s.wakservice.cfg.ini";
		// core service generic
		$files[]=WAK_CORE_PATH."cfg/$s.wakservice.cfg.ini";
		// trying files
		foreach ($files as $file) {
			if (file_exists($file) and $cfg=parse_ini_file($file,true)) {
				// merging new config with default one
				$this->cfg=$this->array_merge_recursive_replace($this->cfg,$cfg);
				//trigger_error("loaded config from $file");
				return true;
				break;
			}
		}
		return false;
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
